package com.dgty.dg_common.util

import android.content.Context
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.load.resource.bitmap.CircleCrop


/**
 * Title: GlideUtils.kt
 * Description:
 * Created DateTime: 2023-02-22
 * Created by xxl.
 */
object GlideUtils {
    /**
     * 加载图片
     *
     * @param mContext 上下文
     * @param url      图片地址
     * @param iv_icon  承载图片的UI框架
     */
    fun loadImg(mContext: Context?, url: Any?, iv_icon: ImageView?, @DrawableRes resourceId: Int) {
        Glide.with(mContext!!)
            .load(url)
            .error(resourceId)
            .into(iv_icon!!)
    }
    /**
     * 加载图片
     *
     * @param mContext 上下文
     * @param url      图片地址
     * @param iv_icon  承载图片的UI框架
     */
    fun loadImg(mContext: Context?, url: Any?, iv_icon: ImageView?) {
        Glide.with(mContext!!)
            .load(url)
//            .error(resourceId)
            .into(iv_icon!!)
    }

    /**
     * 加载圆角图片
     *
     * @param mContext       上下文
     * @param url            图片地址
     * @param roundingRadius 圆角大小
     * @param iv_icon        承载图片的UI框架
     */
    @JvmStatic
    fun loadImg(mContext: Context?, url: Any?, roundingRadius: Int, iv_icon: ImageView?, @DrawableRes resourceId: Int) {

        //设置图片圆角角度
        val roundOptions = RequestOptions().transform(RoundedCorners(roundingRadius))
        //处理CenterCrop的情况,保证圆角不失效
        roundOptions.transform(CenterCrop(), RoundedCorners(roundingRadius))

        Glide.with(mContext!!)
            .load(url)
            .apply(roundOptions)
            .placeholder(resourceId)
            .into(iv_icon!!)
    }

    /**
     * 加载圆形图片
     *
     * @param mContext 上下文
     * @param url      图片地址
     * @param iv_icon  承载图片的UI框架
     */
    @JvmStatic
    fun loadCircularImg(mContext: Context?, url: Any?, iv_icon: ImageView?, @DrawableRes resourceId: Int) {
        Glide.with(mContext!!)
            .load(url)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .placeholder(resourceId)
            .into(iv_icon!!)
    }


}